package justtry.sort;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择排序 将未排序中选出最小的一位 放在当前位置处
 * Created by weishaofei on 2017/12/10.
 */
public class SelectSort {

    public static void sort(List<Integer> list) {
        for (int index = 0; index < list.size(); index++) {
            int minIndex = 0;
            boolean findMin = false;
            int minValue = list.get(index);
            for (int subIndex = index; subIndex < list.size(); subIndex++) {//获取最小值
                if (list.get(subIndex) < minValue){
                    minIndex = subIndex;
                    minValue = list.get(subIndex);
                    findMin = true;
                }
            }
            if (findMin){//插入最小值
                int temp = list.get(index);
                list.set(index, list.get(minIndex));
                list.set(minIndex, temp);
            }
        }
    }


    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(49);
        list.add(39);
        list.add(169);
        list.add(1);
        list.add(10);
        sort(list);
        System.out.println(list);
    }
}
